//
//  DownloadedViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-11.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "DownloadedViewController.h"
#import "DownloadedBookCell.h"
#import "DownloadedChapterViewController.h"

@interface DownloadedViewController ()

@end

@implementation DownloadedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.booksToDeleteArray = [NSMutableArray arrayWithCapacity:0];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:kOneDownloadFinished object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    downloadedGridView.requiresSelection = NO;
    downloadedGridView.backgroundViewExtendsUp = YES;
    downloadedGridView.backgroundViewExtendsDown = YES;
    
    nothingLabel.hidden = YES;
    nothingUnpaidLabel.hidden = YES;
    
    [self updateDisplay];
}

- (void)updateDisplay
{
    self.downloadedArray = [[DownloadManager defaultManager] getDownloadedID];
    [downloadedGridView reloadData];
    if (_downloadedArray.count > 0) {
        nothingLabel.hidden = YES;
        nothingUnpaidLabel.hidden = YES;
    }
    else {
        if ([[ActivateManager shareManager] isActivated]) {
            nothingLabel.hidden = NO;
            nothingUnpaidLabel.hidden = YES;
        }
        else {
            nothingLabel.hidden = YES;
            nothingUnpaidLabel.hidden = NO;
        }
    }
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{
    return self.downloadedArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    DownloadedBookCell *cell = (DownloadedBookCell *)[downloadedGridView dequeueReusableCellWithIdentifier:@"DownloadedBookCell"];
    if (cell == nil) {
        cell = [[DownloadedBookCell alloc] initWithFrame:CGRectMake(0, 0, 96, 128) reuseIdentifier:@"DownloadedBookCell"];
        cell.selectionStyle = AQGridViewCellSelectionStyleBlue;
        cell.booksToDeleteArray = _booksToDeleteArray;
    }
    cell.item_id = [self.downloadedArray objectAtIndex:index];
    [cell updateDisplay];
    return cell;
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    if (downloadedGridView.editing) {
        [downloadedGridView deselectItemAtIndex:index animated:NO];
    }
    else {
        DownloadedChapterViewController *downloadedChapterViewController = [[[DownloadedChapterViewController alloc] initWithNibName:@"DownloadedChapterViewController" bundle:nil] autorelease];
        downloadedChapterViewController.item_id = [self.downloadedArray objectAtIndex:index];
        
        UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
        [navController pushViewController:downloadedChapterViewController animated:YES];
        [downloadedGridView deselectItemAtIndex:index animated:NO];
    }
}

- (void)beginEdit
{
    downloadedGridView.editing = YES;
}

- (void)completeEdit
{
    downloadedGridView.editing = NO;
}

- (void)selectAll
{
    if (_booksToDeleteArray.count == _downloadedArray.count) {
        [_booksToDeleteArray removeAllObjects];
    }
    else {
        [_booksToDeleteArray removeAllObjects];
        [_booksToDeleteArray addObjectsFromArray:_downloadedArray];
    }
    [downloadedGridView reloadData];
}

- (void)deleteSelected
{
    for (NSString *itemID in _booksToDeleteArray) {
        //如果将要删除的正在播放，则停止播放
        if ([[[[AudioPlayer defaultPlayer] currentItem] objectForKey:@"item_id"] isEqualToString:itemID]) {
            [[AudioPlayer defaultPlayer] stop];
        }
        [[DownloadManager defaultManager] deleteBookID:itemID];
    }
    [_booksToDeleteArray removeAllObjects];
    [self updateDisplay];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_downloadedArray release];
    [downloadedGridView release];
    [_booksToDeleteArray release];
    [nothingUnpaidLabel release];
    [nothingLabel release];
    [super dealloc];
}
- (void)viewDidUnload {
    [downloadedGridView release];
    downloadedGridView = nil;
    [nothingUnpaidLabel release];
    nothingUnpaidLabel = nil;
    [nothingLabel release];
    nothingLabel = nil;
    [super viewDidUnload];
}
@end
